package com.dragon.english_practice_back.controller;

import com.dragon.english_practice_back.reponse.Result;
import com.dragon.english_practice_back.service.AuthenticationUtil;
import com.dragon.english_practice_back.service.GrammarsScheduleService;
import com.dragon.english_practice_back.service.GrammarsService;
import com.dragon.english_practice_back.service.TableAndColumnNameVerifyService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/grammars")
@Slf4j
@AllArgsConstructor
public class GrammarsController {

    GrammarsService grammarsService;
    TableAndColumnNameVerifyService tableAndColumnNameVerifyService;
    AuthenticationUtil authenticationUtil;
    GrammarsScheduleService grammarsScheduleService;

    @PreAuthorize("hasAuthority('ROLE_USER')")
    @RequestMapping("/getGrammars")
    public Result<Object> getGrammars(){

        List<Map<String, Object>> grammarsSchedule = grammarsScheduleService.getGrammarsSchedule(authenticationUtil.id());
        return Result.ok("grammars",Map.of("records",grammarsSchedule));
    }

    @PreAuthorize("hasAuthority('ROLE_USER')")
    @GetMapping("/testGrammarsSchedule")
    public Result<Object> testGrammarsSchedule(){
        // 测试
        List<Map<String, Object>> grammarsSchedule = grammarsScheduleService.getGrammarsSchedule(authenticationUtil.id());
        List<Map<String, Object>> randomGrammars = grammarsScheduleService.getRandomGrammars(authenticationUtil.id(), 10);
        int maxGrammarsSize = grammarsScheduleService.getMaxGrammarsSize();
        grammarsScheduleService.updateToDatabase();

        return Result.ok("test", Map.of("grammarsSchedule", grammarsSchedule, "randomGrammars", randomGrammars, "maxGrammarsSize", maxGrammarsSize ));

    }

}
